package com.github.prontera.controller;


import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.prontera.ApiVersion;
import com.github.prontera.common.filter.CurrentUserId;
import com.github.prontera.domain.Client;
import com.github.prontera.domain.ClientAccount;
import com.github.prontera.domain.ClientInvitation;
import com.github.prontera.domain.Consume;
import com.github.prontera.domain.IssueAnswer;
import com.github.prontera.domain.ZClient;
import com.github.prontera.domain.enums.ConsumeLevelTypeEnum;
import com.github.prontera.domain.enums.ConsumeSmallTypeEnum;
import com.github.prontera.domain.enums.RegisterChannelEnum;
import com.github.prontera.domain.enums.UserStatusEnum;
import com.github.prontera.model.request.DeleteImageRequest;
import com.github.prontera.model.response.ObjectDataResponse;
import com.github.prontera.model.response.Response;
import com.github.prontera.service.ClientAccountService;
import com.github.prontera.service.ClientInvitationService;
import com.github.prontera.service.ClientService;
import com.github.prontera.service.ConsumeService;
import com.github.prontera.service.IssueAnswerService;
import com.github.prontera.util.Page;

import io.swagger.annotations.ApiOperation;

/**
 * 用户管理
 * @author Mengxiangdong
 */

@RestController
@RequestMapping(value = ApiVersion.MANAGE_V1_PREFIX + "/client", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, consumes = { MediaType.APPLICATION_JSON_UTF8_VALUE })
public class ClientController {
	
	@Autowired
	private ClientService clientService;
	@Autowired
	private IssueAnswerService issueAnswerService;
	@Autowired
	private ClientInvitationService clientInvitationService;
	
	@Autowired
	private ConsumeService consumeService;
	
	@Autowired
	private ClientAccountService clientAccountService;

	@ApiOperation(value = "上线", notes = "查询用户详情")
	@RequestMapping(value = "/pushClient", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Boolean> pushClient(@RequestBody Client client) {
		client.setStatus(UserStatusEnum.NOMAIL);
		client.setHasHidden(1);
		clientService.update(client);
		return new ObjectDataResponse<Boolean>(true);
	}
	
	@ApiOperation(value = "查询用户列表", notes = "查询用户列表")
	@RequestMapping(value = "/queryClient", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<Client>> queryRole(@RequestBody Client client) {

		client.setOrderBy("U_CLIENT.create_time desc");
		Page<Client> datas = clientService.page(client);
		return new ObjectDataResponse<Page<Client>>(datas);
	}
	
	@ApiOperation(value = "删除图片", notes = "删除图片")
	@RequestMapping(value = "/deleteImage", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Boolean> deleteImage(@RequestBody DeleteImageRequest deleteImageRequest) {
		Client client=clientService.get(deleteImageRequest.getId());
		if(client!=null&&!StringUtils.isEmpty(client.getPhotos())){
			String[] imags=client.getPhotos().split("[,]");
			List<String> images=new ArrayList<String>();
			for (String img : imags) {
				if(!img.equals(deleteImageRequest.getImage())){
					images.add(img);
				}
			}
			client.setPhotos(StringUtils.join(images,","));
			clientService.update(client);
		}
		return new ObjectDataResponse<Boolean>(true);
	}
	
	@ApiOperation(value = "查询用户详情", notes = "查询用户详情")
	@RequestMapping(value = "/getClient", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Client> getClient(@RequestBody Client client) {
		client= clientService.get(client.getId());
		IssueAnswer  issueAnswerObj=issueAnswerService.findByClientId(client.getId());
		client.setIssueAnswer(issueAnswerObj);
		return new ObjectDataResponse<Client>(client);
	}
	
	@ApiOperation(value = "新增用户列表", notes = "新增用户列表")
	@RequestMapping(value = "/addClient", method = RequestMethod.POST)
	@ResponseBody
	public Response addClient(@RequestBody Client client) {
		
		client.setStatus(UserStatusEnum.NOMAIL);
		client.setRegisterChannel(RegisterChannelEnum.PC);
		int success=clientService.insert(client);
		return new ObjectDataResponse<Integer>(success);
	}
	
	@ApiOperation(value = "删除用户", notes = "删除用户")
	@RequestMapping(value = "/deleteClient", method = RequestMethod.POST)
	@ResponseBody
	public Response deleteClient(@CurrentUserId Long userId, @RequestBody Client client) {
		int success = clientService.remove(client);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", success);
		if(success == 2){
			map.put("msg", "该角色被用户所使用，无法被删除！");	
		}
		return new ObjectDataResponse<Object>(map);
	}
	
	
	@ApiOperation(value = "查询用户上下级关系", notes = "查询用户上下级关系")
	@RequestMapping(value = "/queryPassive", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<ClientInvitation>> queryPassive(@RequestBody ClientInvitation clientInvitation) {
		
		Page<ClientInvitation> datas = clientInvitationService.invitationPage(clientInvitation);
		return new ObjectDataResponse<Page<ClientInvitation>>(datas);
	}
	
	@ApiOperation(value = "实名认证", notes = "实名认证")
	@RequestMapping(value = "/updateClient", method = RequestMethod.POST)
	@ResponseBody
	public Response updateClient(@RequestBody Client client) {
		client.setRealTime(OffsetDateTime.now());
		int success = clientService.update(client);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", success);
		if(success == 2){
			map.put("msg", "实名认证失败！");	
		}
		return new ObjectDataResponse<Object>(map);
	}
	
	@ApiOperation(value = "余额提现", notes = "余额提现")
	@RequestMapping(value = "/withdrawals", method = RequestMethod.POST)
	@ResponseBody
	public Response withdrawals(@RequestBody Client client) {
		client.setAmount(client.getAmount().subtract(client.getLocakedCashAmount()));
		int success = clientService.update(client);
		if(success == 1){
			Consume consume=new Consume();
			consume.setClientId(client.getId());
			consume.setLevelType(ConsumeLevelTypeEnum.BALANCE);
			consume.setAmount(client.getLocakedCashAmount());
			consume.setStatus(2);
			consume.setConsumeType(ConsumeSmallTypeEnum.DEPOSIT);
			consumeService.insert(consume);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", success);
		if(success == 2){
			map.put("msg", "提现申请失败！");	
		}
		return new ObjectDataResponse<Object>(map);
	}
	

	@ApiOperation(value = "查询帐户列表", notes = "查询帐户列表")
	@RequestMapping(value = "/queryAccount", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<ClientAccount>> queryAccount(@RequestBody ClientAccount clientAccount) {

		clientAccount.setOrderBy("U_CLIENT_ACCOUNT.create_time desc");
		Page<ClientAccount> datas = clientAccountService.page(clientAccount);
		return new ObjectDataResponse<Page<ClientAccount>>(datas);
	}
	
	public static void main(String[] args) {
		String test="http://106.14.161.174/chsi/5.jpg,http://106.14.161.174/chsi/6.jpg,http://106.14.161.174/chsi/6.jpg,http://106.14.161.174/chsi/6.jpg,http://106.14.161.174/chsi/6.jpg,http://106.14.161.174/chsi/5.jpg,http://106.14.161.174/chsi/5.jpg,http://106.14.161.174/chsi/5.jpg";
		String[] str=test.split(",");
		System.out.println(str.length);
	}

}
